package com.example.as_sum.activity;

import android.os.Bundle;
import android.os.Handler;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.widget.TextView;

import com.example.as_sum.R;
import com.example.as_sum.adapter.MyRecycleViewAdapter;
import com.example.as_sum.entity.ShopInfo;
import com.example.as_sum.event.ShopClickEvent;
import com.example.as_sum.event.ShopCollectEvent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

public class RecycleView2Activity extends BaseActivity {
    @Bind(R.id.test_listview)
    RecyclerView mListvIew;
    MyRecycleViewAdapter mRecycleViewAdapter;
    List<ShopInfo> mShopInfos;
    @Bind(R.id.test_tv_edit)
    TextView mTestTvEdit;
    boolean isAllShow;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    public int getContentId() {
        return R.layout.activity_recycle_view2;
    }

    @Override
    public void initView() {
        mShopInfos = new ArrayList<>();
        for (int x = 0; x < 30; x++) {
            ShopInfo shopInfo = new ShopInfo(100 + x, "李发染" + x, "他是一个很帅的人哎" + x, 100 + x);
            mShopInfos.add(shopInfo);
        }
        mRecycleViewAdapter = new MyRecycleViewAdapter(this, mShopInfos);
        mListvIew.setLayoutManager(new LinearLayoutManager(this));
//        mListvIew.addItemDecoration();
        mListvIew.setAdapter(mRecycleViewAdapter);
    }

    @Subscribe
    public void onEventMainThread(ShopClickEvent event) {
        for (ShopInfo shopInfo : mShopInfos) {
            if (shopInfo.isState()) {
                Log.e("---->", "选中的商品id：" + shopInfo.getSid());
            }
        }
    }
    @Subscribe
    public void onEventMainThread(ShopCollectEvent event) {
        int index=event.getPosition();
        ShopInfo shopInfo=mShopInfos.get(index);
        if(event.isStatus()){
            shopInfo.setCollect(true);
            shopInfo.setCount(mShopInfos.get(index).getCount()+1);
        }else{
            shopInfo.setCollect(false);
            shopInfo.setCount(shopInfo.getCount()-1);
        }
//        mRecycleViewAdapter.notifyItemChanged(index);
        mRecycleViewAdapter.notifyItemChanged(index,shopInfo);
    }


    @Override
    public void addListener() {

    }

    @Override
    public void loadData() {

    }

    @Override
    public Handler getHandler() {
        return null;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @OnClick(R.id.test_tv_edit)
    public void onClick() {
        if(isAllShow){
            isAllShow=false;
            mTestTvEdit.setText("编辑");
            mRecycleViewAdapter.setAllShow(false);
        }else{
            isAllShow=true;
            mTestTvEdit.setText("取消");
            mRecycleViewAdapter.setAllShow(true);
        }
    }
}
